package com.amc.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.JMSException;

import com.amc.dao.IMerchantDao;
import com.amc.dao.impl.MerchantDaoImpl;
import com.amc.entity.Merchant;
import com.amc.jms.Producer;
import com.amc.service.IMerchantService;
import com.amc.utils.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

public class MerchantServiceImpl implements IMerchantService {
	private IMerchantDao dao = new MerchantDaoImpl();
	private SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
	@Override
	public Merchant queryByMerchantId(String merchantId) {
		return dao.queryByMerchantId(merchantId);
	}

	@Override
	public List<Merchant> query(String isBlack, String merchantName) {
		
		return  dao.query(isBlack, merchantName);
	}

	@Override
	public boolean update(Merchant merchant) {
		boolean result = dao.update(merchant);
		Map<String, String> jmsMap = new HashMap<>();
		if(result){
			merchant = dao.queryByMerchantId(merchant.getMerchantId());
			jmsMap.put("isBlack", merchant.getIsBlack()+"");
			jmsMap.put("merchantId", merchant.getMerchantId());
			try {
				Producer.send(jmsMap);
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public boolean delete(String merchantId) {
		return dao.delete(merchantId);
	}

	@Override
	public String getMerchantIdList() {
		List<String> list = dao.getMerchantIdList();
		return JsonUtil.listToString(list);
	}

	@Override
	public boolean insert(Merchant merchant) {
		return dao.insert(merchant);
	}

	@Override
	public boolean merchantApply(Merchant merchant) {
		merchant.setLastApplyTime(new Date().getTime());
		Merchant m = dao.queryByMerchantId(merchant.getMerchantId());
		if(m != null){
			return dao.update(merchant);
		}else{
			return dao.insert(merchant);
		}
	}

}
